package com.genius.service.impl;

import com.genius.mapper.RoleDeptMapper;
import com.genius.service.RoleDeptService;
import com.genius.system.pojo.Role;
import com.genius.system.pojo.RoleDept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class RoleDeptServiceImpl implements RoleDeptService {

    @Autowired
    private RoleDeptMapper roleDeptMapper;

    @Override
    @Transactional
    public int insertRoleDept(Role role) {
        roleDeptMapper.deleteRoleDeptByRoleId(role.getRoleId());
        Long[] deptIds = role.getDeptIds();
        if (deptIds != null && deptIds.length > 0 && role.getDataAuth().equals("2")) {
            // 新增角色机构关系
            List<RoleDept> list = new ArrayList<>();
            for (Long deptId : deptIds) {
                RoleDept roleDept = new RoleDept();
                roleDept.setRoleId(role.getRoleId());
                roleDept.setDeptId(deptId);
                list.add(roleDept);
            }
            return roleDeptMapper.insertRoleDept(list);
        }
        return 0;
    }

    @Override
    public List<String> selectDeptByRoleId(Long roleId) {
        return roleDeptMapper.selectDeptByRoleId(roleId);
    }
}
